<template>
  <div class="warehouse-container">
    <h2>仓库信息</h2>
    <div class="category-card">
      <div class="category-title">仓库状态</div>
      <div class="warehouse-status">
        <div>总容量: {{ warehouseData.capacity || 0 }}</div>
        <div>已使用: {{ (warehouseData.capacity || 0) - (warehouseData.freeSpace || 0) }}/{{ warehouseData.capacity || 0 }}</div>
        <div>剩余空间: {{ warehouseData.freeSpace || 0 }}</div>
      </div>

      <div class="category-title">产品列表</div>
      <div class="products-grid">
        <div v-for="(quantity, productName) in warehouseData.items" :key="productName" class="product-item">
          <div class="product-icon">📦</div>
          <div class="product-name">{{ productName }}</div>
          <div class="product-count">{{ quantity }}个</div>
        </div>
        <div v-if="!Object.keys(warehouseData.items || {}).length" class="no-products">
          仓库为空
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import { useSSE } from '@/utils/sse';

interface WarehouseData {
  capacity: number;
  size: number;
  freeSpace: number;
  items: Record<string, number>;
}

const warehouseData = ref<WarehouseData>({
  capacity: 0,
  size: 0,
  freeSpace: 0,
  items: {}
});
let sseClient: any = null;

onMounted(() => {
  // 建立SSE连接获取仓库实时数据
  sseClient = useSSE('/view/warehouse', (event) => {
    warehouseData.value = JSON.parse(event.data);
  });
});

onUnmounted(() => {
  // 关闭SSE连接
  sseClient?.close();
});
</script>

<style scoped>
/* 从templates/warehouse.html复制并转换为scoped样式 */
.warehouse-container {
  padding: 20px;
}

.category-card {
  background-color: #fff;
  border: 1px solid #ccc;
  border-radius: 10px;
  padding: 20px;
  margin: 10px auto;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  position: relative;
  width: 100%;
  max-width: 1200px;
  box-sizing: border-box;
}

.category-title {
  font-weight: bold;
  font-size: 18px;
  color: #333;
  margin-bottom: 15px;
  text-align: center;
}

.warehouse-status {
  text-align: center;
  margin-bottom: 20px;
  padding: 10px;
  background-color: #f9f9f9;
  border-radius: 5px;
}

.products-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  margin-top: 10px;
  justify-content: center;
}

.product-item {
  background-color: #f9f9f9;
  border-radius: 5px;
  padding: 10px;
  text-align: center;
  width: 120px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.05);
  transition: transform 0.2s ease-in-out;
}

.product-item:hover {
  transform: scale(1.05);
}

.product-icon {
  font-size: 24px;
  margin-bottom: 5px;
}

.product-name {
  font-size: 14px;
  color: #555;
  margin-bottom: 5px;
}

.product-count {
  font-weight: bold;
  color: #333;
  background-color: #e8f5e9;
  border-radius: 10px;
  padding: 2px 0;
  font-size: 12px;
}

.no-products {
  text-align: center;
  width: 100%;
  padding: 20px;
  color: #666;
}
</style>
